package com.mediacloud.app.newsmodule.activity.audio;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.drawable.ClipDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.GradientDrawable;
import android.graphics.drawable.LayerDrawable;
import android.os.Build;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.Gravity;

import androidx.appcompat.widget.AppCompatSeekBar;

import com.chinamcloud.audioserver.APSLifecycleKit;

public class IndicatorSeekBar extends AppCompatSeekBar {

    // 画笔
    private Paint mPaint;
    // 进度文字位置信息
    private Rect mProgressTextRect = new Rect();
    // 滑块按钮宽度
    private int mThumbWidth = dp2px(80);
    // 滑块按钮高度
    private int mThumbHeight = dp2px(24);
    //用于前后的间距
    private int mThumbWidthExt = dp2px(20);
    private String durationStr = "00:00";
    private boolean showHour = false;
    //滑块按钮颜色
    private int mThumbColor = Color.parseColor("#35C5CE");
    //进度条背景色
    private int progressBackgroundColor = Color.parseColor("#EEEEEE");

    //缓冲进度条颜色
    private int secondaryProgressColor = Color.parseColor("#CCCCCC");
    //进度条前景色
    private int progressForegroundColor = Color.parseColor("#35C5CE");
    //进度文字颜色
    private int progressTextColor = Color.WHITE;

    public IndicatorSeekBar(Context context) {
        this(context, null);
    }

    public IndicatorSeekBar(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public IndicatorSeekBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            GradientDrawable background = new GradientDrawable();
            background.setColor(progressBackgroundColor);
            background.setCornerRadius(dp2px(5));


            GradientDrawable secondaryProgress0 = new GradientDrawable();
            secondaryProgress0.setColor(secondaryProgressColor);
            secondaryProgress0.setCornerRadius(dp2px(5));

            ClipDrawable secondaryProgress1 = new ClipDrawable(secondaryProgress0, Gravity.START, ClipDrawable.HORIZONTAL);


            GradientDrawable progress0 = new GradientDrawable();
            progress0.setColor(progressForegroundColor);
            progress0.setCornerRadius(dp2px(5));

            ClipDrawable progress1 = new ClipDrawable(progress0, Gravity.START, ClipDrawable.HORIZONTAL);
            Drawable[] drawables = new Drawable[3];
            drawables[0] = background;
            drawables[1] = secondaryProgress1;
            drawables[2] = progress1;
            LayerDrawable drawable = new LayerDrawable(drawables);
            drawable.setId(0,android.R.id.background);
            drawable.setId(1,android.R.id.secondaryProgress);
            drawable.setId(2,android.R.id.progress);
            setProgressDrawableTiled(drawable);
        }
        setSplitTrack(false);
        mPaint = new TextPaint();
        mPaint.setAntiAlias(true);
        mPaint.setColor(progressTextColor);
        mPaint.setTextSize(sp2px(12));
        String defaultStr = "00:00/00:00";
        mPaint.getTextBounds(defaultStr, 0, defaultStr.length(), mProgressTextRect);
        mThumbWidth = mProgressTextRect.width() + mThumbWidthExt;
        GradientDrawable drawable = new GradientDrawable();
        drawable.setColor(mThumbColor);
        drawable.setSize(mThumbWidth, mThumbHeight);
        drawable.setCornerRadius(mThumbHeight * 1f / 2);
        setThumb(drawable);

        setPadding(0, 0, 0, 0);
        setThumbOffset(0);
        // 如果不设置padding，当滑动到最左边或最右边时，滑块会显示不全
        //  setPadding(width / 2, 0, width / 2, 0);

    }

    private void resetSize(int max) {
        if (max >= 3600) {
            if (showHour) return;
            showHour = true;
            String defaultStr = "00:00:00/00:00:00";
            mPaint.getTextBounds(defaultStr, 0, defaultStr.length(), mProgressTextRect);
            mThumbWidth = mProgressTextRect.width() + mThumbWidthExt;
            GradientDrawable drawable = new GradientDrawable();
            drawable.setColor(mThumbColor);
            drawable.setSize(mThumbWidth, mThumbHeight);
            drawable.setCornerRadius(mThumbHeight * 1f / 2);
            setThumb(drawable);
            setPadding(0, 0, 0, 0);
            setThumbOffset(0);
            //    setPadding(width / 2, 0, width / 2, 0);
        } else {
            if (!showHour) return;
            showHour = false;
            String defaultStr = "00:00/00:00";
            mPaint.getTextBounds(defaultStr, 0, defaultStr.length(), mProgressTextRect);
            mThumbWidth = mProgressTextRect.width() + mThumbWidthExt;
            GradientDrawable drawable = new GradientDrawable();
            drawable.setColor(mThumbColor);
            drawable.setSize(mThumbWidth, mThumbHeight);
            drawable.setCornerRadius(mThumbHeight * 1f / 2);
            setThumb(drawable);
            setPadding(0, 0, 0, 0);
            setThumbOffset(0);
            // 如果不设置padding，当滑动到最左边或最右边时，滑块会显示不全
            //     setPadding(width / 2, 0, width / 2, 0);
        }


    }

    @Override
    public synchronized void setMax(int max) {
        super.setMax(max);
        resetSize(max);
        durationStr = APSLifecycleKit.turnTime(max);
    }

    @Override
    protected synchronized void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        String progressText;
        if (getMax() >= 3600) {
            progressText = APSLifecycleKit.turnTimeAlwaysShowHour(getProgress()) + "/" + durationStr;
        } else {
            progressText = APSLifecycleKit.turnTime(getProgress()) + "/" + durationStr;
        }

        mPaint.getTextBounds(progressText, 0, progressText.length(), mProgressTextRect);

        // 进度百分比
        float progressRatio = (float) getProgress() / getMax();
        // thumb偏移量
        float thumbOffset = (mThumbWidth - mProgressTextRect.width()) / 2 - mThumbWidth * progressRatio;
        float thumbX = getWidth() * progressRatio + thumbOffset;
        //2px用于文字的偏移量 文字的垂直中心点并不是文字高度/2 具体去看FontMetrics 应该通过FontMetrics获取真实的垂直中心点
        float thumbY = getHeight() / 2f + mProgressTextRect.height() / 2f - 2;
        canvas.drawText(progressText, thumbX, thumbY, mPaint);
    }

    /**
     * dp转px
     *
     * @param dp dp值
     * @return px值
     */
    public int dp2px(float dp) {
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp,
                getResources().getDisplayMetrics());
    }

    /**
     * sp转px
     *
     * @param sp sp值
     * @return px值
     */
    private int sp2px(float sp) {
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, sp,
                getResources().getDisplayMetrics());
    }
}
